<template>
  <div class="mains">
    <!-- 装饰元素 -->
    <div class="deco-leaves">
      <div class="leaf leaf-1">🌿</div>
      <div class="leaf leaf-2">🍃</div>
      <div class="leaf leaf-3">🌾</div>
      <div class="leaf leaf-4">🌱</div>
    </div>
    
    <div class="loginbox">
      <!-- Logo区域 -->
      <div class="logo-section">
        <div class="logo-icon">🏡</div>
        <div class="tips">Hi！欢迎使用</div>
        <div class="tips1">农场管理系统</div>
        <div class="subtitle">Farm Management System</div>
      </div>
      
      <el-form :model="info" :rules="rules" ref="ruleForm" hide-required-asterisk label-width="" class="demo-ruleForm">
        <el-form-item label="" prop="name" style="width: 100%;">
          <div class="input-wrapper">
            <i class="input-icon el-icon-user-solid"></i>
            <el-input v-model="info.name" placeholder="请输入用户名"></el-input>
          </div>
        </el-form-item>
        
        <el-form-item label="" prop="pwd" style="width: 100%;">
          <div class="input-wrapper">
            <i class="input-icon el-icon-lock"></i>
            <el-input v-model="info.pwd" type="password" placeholder="请输入密码" @keyup.enter.native="submitForm('ruleForm')"></el-input>
          </div>
        </el-form-item>
        
        <el-form-item label="" prop="code" style="width: 100%;" class="code-item">
          <div class="input-wrapper code-wrapper">
            <i class="input-icon el-icon-picture"></i>
            <el-input v-model="info.code" type="text" placeholder="请输入验证码" @keyup.enter.native="submitForm('ruleForm')"></el-input>
            <div class="code-image" @click="getCode">
              <identify :identifyCode="identifyCode"></identify>
            </div>
          </div>
        </el-form-item>
        
        <el-form-item style="width: 100%;">
          <el-button type="success" @click="submitForm('ruleForm')" class="bth">
            <span>登 录</span>
            <i class="el-icon-right"></i>
          </el-button>
        </el-form-item>
      </el-form>
      
      <!-- 底部装饰 -->
      <div class="footer-deco">
        <span class="deco-item">🌻</span>
        <span class="deco-item">🌳</span>
        <span class="deco-item">🌻</span>
      </div>
    </div>
  </div>
</template>
<script>
import identify from "@/components/identify";
import { captchaImage, login } from "./util/api.js"
export default {
  components: {
    identify
  },
  data() {
    return {
      info: {
        name: "13895674789",//13895674789
        pwd: "123456",//123456
        code: "",
        uuid: ""
      },
      rules: {
        name: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        code:[
        { required: true, message: '验证码', trigger: 'blur' },
        ]
      },
      identifyCode: ""
    }
  },
  mounted() {
    this.getCode()
    window.addEventListener('keydown', this.keyDown)
  },
  beforeDestroy(){
    window.removeEventListener('keydown', this.keyDown, false)
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await login(this.info)
          if (res.code != 200) {
            this.$message.error(res.msg)
            this.getCode()
          } else {
            window.sessionStorage.setItem("name", res.data.name)
            window.sessionStorage.setItem("roleName", res.data.departmentName)
            window.sessionStorage.setItem("token", res.data.token)
            window.sessionStorage.setItem("headImgUrl", res.data.headImgUrl)
            window.sessionStorage.setItem('routes', JSON.stringify(res.data.resourceChildren))
            this.$router.push({ path: '/home' })
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    async getCode() {
      let res = await captchaImage()
      if (res.code != 200) return
      this.info.uuid = res.uuid
      this.identifyCode = res.captcha
    },
    keyDown(e){
      // console.log(e)
      //回车则执行登录方法
      if (e.keyCode == 13) {
        this.submitForm('ruleForm')
      }
    }
  }
}
</script>
<style lang="less" scoped>
@font-face {
  font-family: "SC-xie";
  src: url('./assets/logins.woff');
  font-weight: normal;
  font-style: normal;
}

/* 主题色定义 */
@primary-green: #52c41a;
@deep-green: #389e0d;
@light-green: #95de64;
@bg-green: #f6ffed;
@border-green: #b7eb8f;

*,
body {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.mains {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  background-image: url('./assets/615158eb6df609344.png');
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(82, 196, 26, 0.3) 0%, rgba(56, 158, 13, 0.4) 100%);
    z-index: 1;
  }
}

/* 装饰叶子动画 */
.deco-leaves {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
  
  .leaf {
    position: absolute;
    font-size: 40px;
    opacity: 0.3;
    animation: float 6s ease-in-out infinite;
    
    &.leaf-1 {
      top: 10%;
      left: 10%;
      animation-delay: 0s;
    }
    
    &.leaf-2 {
      top: 20%;
      right: 15%;
      animation-delay: 1.5s;
    }
    
    &.leaf-3 {
      bottom: 15%;
      left: 20%;
      animation-delay: 3s;
    }
    
    &.leaf-4 {
      bottom: 25%;
      right: 10%;
      animation-delay: 4.5s;
    }
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(10deg);
  }
}

.loginbox {
  width: 480px;
  min-height: 600px;
  padding: 40px 50px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(82, 196, 26, 0.3), 0 0 80px rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border: 2px solid rgba(149, 222, 100, 0.3);
  position: relative;
  z-index: 2;
  animation: slideUp 0.6s ease-out;
  
  &::before {
    content: '';
    position: absolute;
    top: -2px;
    left: -2px;
    right: -2px;
    bottom: -2px;
    background: linear-gradient(45deg, @primary-green, @light-green, @primary-green);
    border-radius: 20px;
    opacity: 0;
    transition: opacity 0.3s;
    z-index: -1;
  }
  
  &:hover::before {
    opacity: 0.1;
  }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Logo区域 */
.logo-section {
  text-align: center;
  margin-bottom: 40px;
  
  .logo-icon {
    font-size: 60px;
    margin-bottom: 10px;
    animation: bounce 2s ease-in-out infinite;
  }
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
}

.tips {
  font-size: 20px;
  font-weight: 500;
  color: #52c41a;
  margin-bottom: 5px;
  letter-spacing: 1px;
}

.tips1 {
  font-size: 32px;
  font-weight: 700;
  color: #389e0d;
  font-family: 'SC-xie', sans-serif;
  margin-bottom: 8px;
  text-shadow: 2px 2px 4px rgba(82, 196, 26, 0.1);
}

.subtitle {
  font-size: 13px;
  color: #95de64;
  letter-spacing: 2px;
  font-weight: 300;
}

/* 表单样式 */
.demo-ruleForm {
  width: 100%;
  margin-top: 20px;
  
  .el-form-item {
    margin-bottom: 25px;
  }
}

/* 输入框容器 */
.input-wrapper {
  position: relative;
  width: 100%;
  
  .input-icon {
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 20px;
    color: @primary-green;
    z-index: 1;
    transition: all 0.3s;
  }
  
  &:hover .input-icon {
    color: @deep-green;
    transform: translateY(-50%) scale(1.1);
  }
}

.code-wrapper {
  display: flex;
  align-items: center;
  gap: 10px;
  
  .code-image {
    flex-shrink: 0;
    width: 122px;
    height: 48px;
    border-radius: 6px;
    overflow: hidden;
    border: 2px solid @border-green;
    cursor: pointer;
    transition: all 0.3s;
    background: white;
    
    &:hover {
      border-color: @primary-green;
      box-shadow: 0 2px 8px rgba(82, 196, 26, 0.3);
      transform: scale(1.02);
    }
  }
}

/deep/.el-input {
  .el-input__inner {
    height: 48px;
    line-height: 48px;
    border: 2px solid #e8f5e9;
    border-radius: 10px;
    padding-left: 50px;
    font-size: 15px;
    background: #fafffe;
    transition: all 0.3s;
    
    &:hover {
      border-color: @border-green;
      background: white;
    }
    
    &:focus {
      border-color: @primary-green;
      background: white;
      box-shadow: 0 0 0 3px rgba(82, 196, 26, 0.1);
    }
    
    &::placeholder {
      color: #bfbfbf;
      font-size: 14px;
    }
  }
}

/* 登录按钮 */
.bth {
  width: 100%;
  height: 50px;
  margin-top: 15px;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 4px;
  border-radius: 10px;
  background: linear-gradient(135deg, @primary-green 0%, @deep-green 100%);
  border: none;
  box-shadow: 0 4px 15px rgba(82, 196, 26, 0.3);
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(82, 196, 26, 0.4);
    background: linear-gradient(135deg, @deep-green 0%, @primary-green 100%);
  }
  
  &:active {
    transform: translateY(0);
  }
  
  i {
    font-size: 16px;
    transition: transform 0.3s;
  }
  
  &:hover i {
    transform: translateX(3px);
  }
}

/* 底部装饰 */
.footer-deco {
  text-align: center;
  margin-top: 25px;
  display: flex;
  justify-content: center;
  gap: 30px;
  
  .deco-item {
    font-size: 24px;
    opacity: 0.6;
    animation: sway 3s ease-in-out infinite;
    
    &:nth-child(2) {
      animation-delay: 1s;
    }
    
    &:nth-child(3) {
      animation-delay: 2s;
    }
  }
}

@keyframes sway {
  0%, 100% {
    transform: rotate(-5deg);
  }
  50% {
    transform: rotate(5deg);
  }
}

/* 验证码项特殊处理 */
.code-item {
  /deep/.el-input {
    flex: 1;
    
    .el-input__inner {
      width: 100%;
    }
  }
}
</style>